//
//  diaryViewController.m
//  CustomDateTimePicker
//
//  Created by lanou3g on 15/7/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "diaryViewController.h"
#import "SqliteDtaHandle.h"
#import "diaryTableViewController.h"
@interface diaryViewController ()<UITextViewDelegate>

@end

@implementation diaryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255/256.0 green:204/256.0 blue:204/256.0 alpha:1];
    //打开数据库
    self.dataHandle = [SqliteDtaHandle shareSqliteDataHandle];
    [_dataHandle openSqlite];
    
    
    //布置页面
    [self layoutLabelAndTextView];
    
    
    
}
-(void)layoutLabelAndTextView{
    //设置左侧保存按钮按钮
    UIBarButtonItem *canselBT = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"ca1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:(UIBarButtonItemStylePlain) target:self action:@selector(canselAc:)];
    
    self.navigationItem.leftBarButtonItem = canselBT;
    
    //设置左侧查看所有日记的按钮
    UIBarButtonItem *allDiaryBt =[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"list1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:(UIBarButtonItemStylePlain) target:self action:@selector(lookAllDiary:)];
    self.navigationItem.rightBarButtonItem = allDiaryBt;
    
    //初始化一个textView
    self.diaryText = [[UITextView alloc]initWithFrame:CGRectMake(20, 80, 340, 500)] ;
    
    //设置背景图片
   // _diaryText.layer.contents = (id)[UIImage imageNamed:@"d.png"].CGImage;
    self.diaryText.delegate = self;
    //返回键的类型
    _diaryText.returnKeyType = UIReturnKeyDone;
    //键盘类型
    _diaryText.keyboardType = UIKeyboardTypeDefault;
    //自适应高度
    _diaryText.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    //设置是否可以滚动
    _diaryText.scrollEnabled = YES;
    //设置字体大小
    _diaryText.font = [UIFont fontWithName:@"Arial" size:17];
    //设置圆角
    _diaryText.layer.cornerRadius = 8;
    //设置边框
    _diaryText.layer.borderWidth = 5;
    _diaryText.layer.borderColor = [[UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:0.5] CGColor];
    
    [self.view addSubview:_diaryText];
    
    //设置保存按钮
    UIButton *keepBt = [UIButton buttonWithType:UIButtonTypeSystem];
    keepBt.frame = CGRectMake(320, 600, 40, 40);
    [keepBt addTarget:self action:@selector(keepToShuJuKu:) forControlEvents:UIControlEventTouchUpInside];
    [keepBt setBackgroundImage:[UIImage imageNamed:@"k1.png"] forState:UIControlStateNormal];
    [self.view addSubview:keepBt];

}
//点击return回收键盘
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder]; return NO;
    }
    return YES; 
}

//隐藏键盘
- (void)resignKeyboard {
    [_diaryText resignFirstResponder];
}

-(void)canselAc:(UIButton *)bt{
    [self dismissViewControllerAnimated:YES completion:nil];

}
//点击空白处完成收回键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_diaryText resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//保存到数据库的方法
-(void)keepToShuJuKu:(UIButton *)bt{
    //添加保存到数据库的方法
    [self.dataHandle downLoadInShuJuKu:[NSDate date] contants:_diaryText.text];
}
//查看所有的日记的方法
-(void)lookAllDiary:(UIBarButtonItem *)bt{
   NSMutableArray *diaryArr = [self.dataHandle selectAllDiary];
    diaryTableViewController *diaryTableView = [[diaryTableViewController alloc]initWithStyle:UITableViewStylePlain];
    diaryTableView.dArray = diaryArr;
    [self.navigationController pushViewController:diaryTableView animated:YES];
}


@end
